package com.by.controller;

import com.by.mapper.QuoteMapper;
import com.by.mapper.SpecificMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class MapperFactory {

    private final Map<String, SpecificMapper<?>> mapperMap = new HashMap<>();

    @Autowired
    public MapperFactory(List<SpecificMapper<?>> mappers) {
        for (SpecificMapper<?> mapper : mappers) {
            String typeName = mapper.getClass().getSimpleName().replace("Mapper", "");
            mapperMap.put(typeName, mapper);
        }
    }

    @SuppressWarnings("unchecked")
    public <T> SpecificMapper<T> getMapper(String type) {
        return (SpecificMapper<T>) mapperMap.get(type);
    }
}
